package me.cxlr.qinlauncher2.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

/**
 * 汉字转拼音
 *
 * @author 酷安用户:山轮纸
 */
public class Han2PinYinUtil {

    public static String getFirstWord(String hanZi) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        // 拼音大写
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        // 不带声调
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        // 要转换的中文,格式,转换之后的拼音的分隔符,遇到不能转换的不保留
        try {
            String tmp = PinyinHelper.toHanYuPinyinString(hanZi, format, ",", true);
            if (tmp.length() > 0) {
                return tmp.substring(0, 1);
            } else {
                return "#";
            }
        } catch (Exception e) {
            return "#";
        }
    }

}
